package com.xiaoshuidi.cloud.module.iot.dal.repository;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoshuidi.cloud.module.iot.dal.mapper.DeviceOperationMapper;
import com.xiaoshuidi.cloud.module.iot.dal.model.doorLock.DoorLockOperationRecord;
import com.xiaoshuidi.cloud.module.iot.dal.model.doorLock.DoorLockReportReqVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.doorLock.DoorLockReportRespVO;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceOperation;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @Author Feng
 * @date 2023/6/16
 */
@Repository
public class DeviceOperationRepository extends ServiceImpl<DeviceOperationMapper, TbDeviceOperation> {


    public int getDeviceOpenRecordNumByDeviceNo(String deviceNo) {
        return this.baseMapper.getDeviceOpenRecordNumByDeviceNo(deviceNo);
    }

    public PageInfo<DoorLockReportRespVO> getOpenDoorRecord(DoorLockReportReqVO doorLockReportReqVO) {
        PageHelper.startPage(doorLockReportReqVO.getPageNum(), doorLockReportReqVO.getPageSize());
        String queryBeginTime = null;
        String queryEndTime = null;
        if (doorLockReportReqVO.getQueryBeginTime() != null && doorLockReportReqVO.getQueryBeginTime() != 0) {
            queryBeginTime = DateUtil.format(new Date(doorLockReportReqVO.getQueryBeginTime()), "yyyy-MM-dd HH:mm:ss");
        }
        if (doorLockReportReqVO.getQueryEndTime() != null && doorLockReportReqVO.getQueryEndTime() != 0) {
            queryEndTime = DateUtil.format(new Date(doorLockReportReqVO.getQueryEndTime()), "yyyy-MM-dd HH:mm:ss");
        }
        QueryWrapper<DoorLockReportReqVO> wrapper = Wrappers.query();
        wrapper.eq("device_no", doorLockReportReqVO.getDeviceNo())
                .eq("status", 1).eq("op_type", 1).eq("deleted",0)
                .ge(StrUtil.isNotEmpty(queryBeginTime), "op_time", queryBeginTime)
                .le(StrUtil.isNotEmpty(queryEndTime), "op_time", queryEndTime)
                .orderByDesc("op_time");
        List<DoorLockReportRespVO> doorLockReportRespVOS = baseMapper.getOpenDoorRecord(wrapper);
        return new PageInfo<DoorLockReportRespVO>(doorLockReportRespVOS);
    }

    public PageInfo<DoorLockOperationRecord> getOperationRecord(DoorLockReportReqVO doorLockReportReqVO) {
        PageHelper.startPage(doorLockReportReqVO.getPageNum(), doorLockReportReqVO.getPageSize());
        String queryBeginTime = null;
        String queryEndTime = null;
        if (doorLockReportReqVO.getQueryBeginTime() != null && doorLockReportReqVO.getQueryBeginTime() != 0) {
            queryBeginTime = DateUtil.format(new Date(doorLockReportReqVO.getQueryBeginTime()), "yyyy-MM-dd HH:mm:ss");
        }
        if (doorLockReportReqVO.getQueryEndTime() != null && doorLockReportReqVO.getQueryEndTime() != 0) {
            queryEndTime = DateUtil.format(new Date(doorLockReportReqVO.getQueryEndTime()), "yyyy-MM-dd HH:mm:ss");
        }
        QueryWrapper<DoorLockReportReqVO> wrapper = Wrappers.query();
        wrapper.eq("device_no", doorLockReportReqVO.getDeviceNo())
                .eq("status", 1).ne("op_type", 1).eq("deleted",0)
                .ge(StrUtil.isNotEmpty(queryBeginTime), "op_time", queryBeginTime)
                .le(StrUtil.isNotEmpty(queryEndTime), "op_time", queryEndTime)
                .orderByDesc("op_time");
        List<DoorLockOperationRecord> dooeLockOperationRecords = baseMapper.getOperationRecord(wrapper);
        return new PageInfo<DoorLockOperationRecord>(dooeLockOperationRecords);
    }
}
